﻿var oracledb = require('oracledb');
var async = require('async');

exports.execute = function (req, res) {
	var doConnect = function(cb) {
	  oracledb.getConnection(
	    {
	      user          : "SCOTT",
	      password      : "TIGER",
	      connectString : localhost/GORACLE
	    },
	    cb);
	};

	var doQueey = function (conn, cb) {
	  conn.execute(
	    "SELECT * FROM emp",
	    {}, 
	    { outFormat: oracledb.OBJECT }, //要求返回为对象格式
	    function(err, result)
	    {
	      if (err) {
	        return cb(err, conn);
	      } else {
	        //console.log(result.rows);
	        res.send(result.rows);
	        return cb(null, conn);
	      }
	    });
	};

	var doRelease = function(conn) {
	  conn.release(function (err) {
	    if (err)
	      console.error(err.message);
	  });
	};

	async.waterfall(
	  [
	    doConnect,
	    doQuery
	  ],
	  function (err, conn) {
	    if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
	    if (conn)
	      doRelease(conn);
	  });
	 
};